using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Common.Core.Configuration
{
	public static class ConfigurationSettings
	{
		static string ResolveName( ICustomAttributeProvider type )
		{
			ConfigurationSettingsNameAttribute[] name = type.GetCustomAttributes( typeof (ConfigurationSettingsNameAttribute), false ) as ConfigurationSettingsNameAttribute[];
			string result = name != null && name.Length == 1 ? name[0].Name : string.Empty;
			return result;
		}

		public static SettingsType Retrieve<SettingsType>( IConfigurationSource configurationSource ) where SettingsType : ConfigurationSection
		{
			SettingsType result = Retrieve<SettingsType>( ResolveName( typeof(SettingsType) ), configurationSource );
			return result;
		}

		public static SettingsType Retrieve<SettingsType>( string name, IConfigurationSource configurationSource ) where SettingsType : ConfigurationSection
		{
			Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNullOrEmpty( name, "name" );
			Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull( name, "configurationSource" );

			return configurationSource.GetSection( name ) as SettingsType;
		}
	}
}